<?php
	defined('INDEX') or die ('Acesso direto não permitido');
	/**
	 * Classe para gerenciar as configura��es de URL do sistema.
	 * 
	 * @author Carlos A. Junior
	 */
	class SEO
	{
		var $request_params = array();
		
		var $object_params  = null;
		
		function SEO()
		{
			$this->object_params = new stdClass();
		}
		
		public function process_seo_request ()
		{
			$requestURI = explode('/', $_SERVER['REQUEST_URI']);
			$scriptName = explode('/', $_SERVER['SCRIPT_NAME']);
			
			$scriptNameArray = array_values($scriptName);
			
			for($i= 0; $i < sizeof($scriptNameArray); $i++) 
			{
				if ($requestURI[$i] == $scriptName[$i])
				{
					unset($requestURI[$i]);
				}
			}
		
			$this->request_params = array_values($requestURI);
			
			# constantes pré-definidas.
			$_GET['pagina'] 				= addslashes($this->request_params[0]);
			$_GET['acao'] 					= addslashes($this->request_params[1]);
			$_GET['id'] 					= addslashes($this->request_params[2]);
			
			$this->object_params->pagina 	= $this->request_params[0];
			$this->object_params->acao 		= $this->request_params[1];
			$this->object_params->id 		= $this->request_params[2];
			
			for($i=3; $i < sizeof($this->request_params); $i++)
			{
				$param = "param_".$i;
				$this->object_params->$param = $this->request_params[$i];
			}
			
			$this->process_post_params();
		}
		
		/**
		 * Esta função faz uma cópia dos parametros do POST para o objeto object_params.
		 * 
		 * @return void
		 */
		private function process_post_params ()
		{
			if(sizeof($_POST))
			{
				foreach ($_POST as $param => $valor)
				{
					$this->object_params->$param = $valor;
				}
			}
		}
		
		/**
		 * This function redirect the main application to an
		 * URL.
		 * 
		 * @param string $url
		 * @return void or false when the headers has been started.
		 */
		public function redirect ($url)
		{
			if(!$url)
			{
				return false;
			}
			else
			{
				if(!headers_sent())
				{
					return header("Location: $url");
				}
				else
				{
					return false;
				}
			}
		}
		
		public function route ($url)
		{
			if(!$url)
			{
				return "";
			}
			else
			{
				$config = new Config();
				if($config->use_sef)
				{
					$parsed_url = preg_replace('/(pagina\=|\&acao=|\&id\=|index.php\?)/i', '/', $url);
					
					return str_replace('//', '/', $parsed_url);
				}
				else
				{
					return $url;
				}
			}
		}
		
		public function get_requested_params ()
		{
			return $this->request_params;
		}
		
		public function get_object_params ()
		{
			return $this->object_params;
		}
	}
?>